var express = require('express');
var router = express.Router();
var multiparty = require('multiparty');
var { messageModel, userModel, stylistModel } = require('../../model/model');


// 修改用户信息
router.post('/edit', async (req, res) => {
    let id = req.query.id;
    let body = req.body.form;
    let data = await userModel.findOne({ _id: id });
    data.uname = body.uname;
    data.sex = body.sex;
    data.email = body.email;
    data.phone = body.phone;
    data.birthday = body.birthday;
    data.imgsrc = body.imgsrc;
    data.save();
    let data2 = await userModel.findOne({ _id: id });
    console.log(data2);
    
    res.send({
        code: 200,
        data: data2
    })
})

// 添加聊天内容
router.post('/add_message', async (req, res) => {
    let body = req.body;
    messageModel.create(body)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})

// 获取聊天内容
router.get('/get_message', async (req, res) => {
    let { from, to } = req.query;
    let query = {
        $or: [
            { from: from, to: to },
            { from: to, to: from }
        ]
    };
    let data = await messageModel.find(query);
    res.send({
        code: 200,
        data: data
    })
})

// 获取全部聊天内容
router.get('/all_message', async (req, res) => {
    let data = await messageModel.find();
    res.send({
        code: 200,
        data: data
    })
})

// 获取用户信息
router.get('/getuser', async (req, res) => {
    let data = await userModel.find();
    res.send({
        code: 200,
        data: data
    })
})


module.exports = router;
